define([
    "jquery","./lang_en.js","./lang_zh.js","../lib/tips/message.js"
], function ($,en,zh,$message) {
    var load1 = function (bajaObj, globalColor) {
        
        $message.message();
        // console.log(bajaObj);
        var data = bajaObj.item;
        if("language" in data){
            if(data["language"]=="zh"){
                var languageData = zh.languageData;
            }else if(data["language"]=="en"){
                var languageData = en.languageData;
            }
        }else{
            var languageData = zh.languageData;
        }
        var scheduleType = bajaObj.item.scheduleType;
        document.getElementById("scheduleRoot").oncontextmenu = function (e) {
            $("#copyLi").show();
            $("#psateLi").show();
            $("#removeWeekLi").show();
            $("#copyMToFLi").show();
            $("#defaultLi").hide();
            $("#divmenu").height(190);
            var leftValue = e.clientX - $("#scheduleRoot").offset().left + 64;
            var topValue = e.clientY - $("#scheduleRoot").offset().top + 71;
            var maxLeft = $("#scheduleRoot").width();
            var maxTop = $("#scheduleRoot").height();
            if (leftValue > maxLeft - 40) {
                leftValue = leftValue - 120;
            }
            if (topValue > maxTop - 40) {
                topValue = topValue - 190;
            }
            if (copyContent != "") {
                $("#psateLi").removeClass("disabledPaste");
            } else if (copyContent == "") {
                $("#psateLi").addClass("disabledPaste");
            }
            $("#divmenu").css({ "left": leftValue + "px", "top": topValue + "px", "display": "block" });
            return false;
        }
        window.onclick = function () {
            $("#divmenu").css({ "display": "none" });
        }
        // $(function () {
        $(".blocksRoot").each(function (item) {
            $(this).on({
                "mousedown": function (event) { mousedown(event, this) },
                "mousemove": function (event) { mousemove(event, this) },
                "mouseup": function (event) { mouseup(event, this) }
            });
        })

        Date.prototype.Format = function (fmt) { //author: meizz
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "H+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }
        document.body.onselectstart = document.body.ondrag = function () {
            return false;
        }
        //动态设置root位置
        var width = $("#weekSchedule").width();
        var single = width / 7 / width * 100 - 1;
        $(".root0").css({ height: 100 + "%", width: single + "%", "border-top": "1px solid #D6D6D6", "border-bottom": "1px solid #D6D6D6" });
        $(".root1").css({ height: 100 + "%", width: single + "%", "border-top": "1px solid #D6D6D6", "border-bottom": "1px solid #D6D6D6" });
        $(".root2").css({ height: 100 + "%", width: single + "%", "border-top": "1px solid #D6D6D6", "border-bottom": "1px solid #D6D6D6" });
        $(".root3").css({ height: 100 + "%", width: single + "%", "border-top": "1px solid #D6D6D6", "border-bottom": "1px solid #D6D6D6" });
        $(".root4").css({ height: 100 + "%", width: single + "%", "border-top": "1px solid #D6D6D6", "border-bottom": "1px solid #D6D6D6" });
        $(".root5").css({ height: 100 + "%", width: single + "%", "border-top": "1px solid #D6D6D6", "border-bottom": "1px solid #D6D6D6" });
        $(".root6").css({ height: 100 + "%", width: single + "%", "border-top": "1px solid #D6D6D6", "border-bottom": "1px solid #D6D6D6" });
        $(".root7").css({ height: 100 + "%", width:  "100%", "border-top": "1px solid #D6D6D6", "border-bottom": "1px solid #D6D6D6" });

        //动态设置block
        var height = $("#weekSchedule").height();
        var single = height / 8 / height * 100;
        $(".block0").css({ top: single * 0 + "%", bottom: single * 7 + "%", width: 100 + "%", height: single + "%", "border-left": "1px solid #D6D6D6", "border-right": "1px solid #D6D6D6" });
        $(".block1").css({ top: single * 1 + "%", bottom: single * 6 + "%", width: 100 + "%", height: single + "%", "border-top": "1px solid #D6D6D6", "border-left": "1px solid #D6D6D6", "border-right": "1px solid #D6D6D6" });
        $(".block2").css({ top: single * 2 + "%", bottom: single * 5 + "%", width: 100 + "%", height: single + "%", "border-top": "1px solid #D6D6D6", "border-left": "1px solid #D6D6D6", "border-right": "1px solid #D6D6D6" });
        $(".block3").css({ top: single * 3 + "%", bottom: single * 4 + "%", width: 100 + "%", height: single + "%", "border-top": "1px solid #D6D6D6", "border-left": "1px solid #D6D6D6", "border-right": "1px solid #D6D6D6" });
        $(".block4").css({ top: single * 4 + "%", bottom: single * 3 + "%", width: 100 + "%", height: single + "%", "border-top": "1px solid #D6D6D6", "border-left": "1px solid #D6D6D6", "border-right": "1px solid #D6D6D6" });
        $(".block5").css({ top: single * 5 + "%", bottom: single * 2 + "%", width: 100 + "%", height: single + "%", "border-top": "1px solid #D6D6D6", "border-left": "1px solid #D6D6D6", "border-right": "1px solid #D6D6D6" });
        $(".block6").css({ top: single * 6 + "%", bottom: single * 1 + "%", width: 100 + "%", height: single + "%", "border-top": "1px solid #D6D6D6", "border-left": "1px solid #D6D6D6", "border-right": "1px solid #D6D6D6" });
        $(".block7").css({ top: single * 7 + "%", bottom: single * 0 + "%", width: 100 + "%", height: single + "%", "border-top": "1px solid #D6D6D6", "border-left": "1px solid #D6D6D6", "border-right": "1px solid #D6D6D6" });

        // 重新给添加虚线
        function addDashedBoard(index){
            var str = "";
            for(var i=0;i<24;i++){
                if((i+1)%3!=0){
                    var topValue = 100*(i/24) + "%";
                    str += "<div class='dashed' style='top:"+topValue+"'></div>"
                }
            }
            for(var i=0;i<8;i++){
                $(".root"+i).append(str);
            }
        }
        addDashedBoard();
        





        //记录鼠标按下时的Y轴位置
        var mousedownY = null;
        //是否创建新事件
        var isCreateEvent = null;
        //选中的Dom
        var checkDom = null;
        //事件类型
        var eventType = null;
        var differ = null;
        var root = null;

        var mouseIsDown = true; // 根据这个，判断鼠标只是按下去，没有移动。
        
        //鼠标点击开始事件
        function mousedown(event, dom) {
            
            $(checkDom).parent().removeClass("active");
            $(checkDom).parent().parent().removeClass("active");
            //在事件div上添加选中class
            // console.log(event.target);
            if($(event.target).hasClass("blocksRoot")){
                return;
            }
            if($(event.target).is("span")){
                $(event.target).parent().parent().addClass("active");
            }else{
                $(event.target).parent().addClass("active");
            }
            
            flag = true;
            mousedownY = getY(event, dom);
            root = dom;
            //div类型
            var divType = $(event.target).data("type");
            //如果等于block，则新建事件
            if (divType == "block") {
                isCreateEvent = true;
                eventType = "newEvent";
                $(".event").each(function () { $(this).removeClass("active") })
            } else {
                setTime(event); // 设置下面的时间
                setColor(event); // 设置下面的事件输出（颜色选择）
                eventType = "moveEvent";
                setTimeout(function(){
                    mouseIsDown = false;
                },100)
                // console.log("鼠标mousedown");
                checkDom = event.target;
                if($(checkDom).is("span")){
                    checkDom = $(checkDom).parent()[0];
                }
                var parentHeight = $(dom).height();
                differ = mousedownY / parentHeight * 100 - $(checkDom).parent().position().top / parentHeight * 100;
                var k = parseInt(event.offsetY / $(checkDom).height() * 100);
                
                if (k > 90) { // 大于滑块的百分之90则是向下放大
                    eventType = "moveBottom";
                }
                if (k < 10) { // 小于百分之10则是，向上放大
                    eventType = "moveTop";
                }
                return false;
            }
        }
        //鼠标点击结束事件
        function mouseup(event, dom) {
            mouseIsDown = true;
            eventType = null;
            // root = null;
            //div类型
            var divType = $(checkDom).data("type");
            //如果等于block，则新建事件
            if (divType == "block") {
                isCreateEvent = null;
            } else {
                //在事件div上添加选中class
                // $(event.target).parent().removeClass("active");
            }
        }
        //其他的地方鼠标谈起，
        $("#schedule").mouseup(function(){
            mouseup();
        })
        //鼠标移动事件
        function mousemove(event, dom) {
            var thingAndThingColor = computerColor();
            var thing = thingAndThingColor.thing;
            var thingColor = thingAndThingColor.thingColor;
            if ($(event.target).hasClass("eventItem")) {
                var k = parseInt(event.offsetY / $(event.target).height() * 100);
                if (k > 90 || k < 10) {
                    // $(event.target).css({ cursor: "row-resize" });
                    $(event.target).css({ cursor: "n-resize" });
                } else {
                    $(event.target).css({ cursor: "pointer" });
                    // $(event.target).css({ cursor: "n-resize" });
                }
            }
            if (!$(root).is($(dom))) {
                return;
            }
            // console.log(eventType);
            //当前root div 的高
            var parentHeight = $(dom).height();
            //获取当前鼠标的Y值
            var currentY = getY(event, dom);
            // console.log("最后得到的事件"+thing)
            //如果创建新事件时
            if (eventType == "newEvent") {
                //top值
                var top = mousedownY / parentHeight * 100;
                //计算出滑动距离
                var height = (currentY - mousedownY) / parentHeight * 100;
                //计算出最少移动距离
                var temp = getTemp(dom);
                //根据top算出bottom总数
                var bottom = 100 - (currentY / parentHeight * 100);
                //获取实际temp总数
                var result = calc(top, temp);
                //如果没有创建过div则创建
                if (isCreateEvent == true) {
                    //获取实际temp总数
                    var difference = calc(height, temp);
                    if (difference > 0) {
                        var s = showTitle(result / temp, (100 - bottom) / temp);
                        var finish = new Date(s.end).Format("HH:mm") == "00:00" ? "24:00":new Date(s.end).Format("HH:mm");
                        var start = new Date(s.start).Format("HH:mm");
                        
                        var divStr = "<div title='"+thing+"\n"+start+"-"+finish + "' data-start= '" + new Date(s.start).getTime() + "' data-end= '" + new Date(s.end).getTime() + "' class = 'event active " + thingColor + "' style='bottom:" + bottom + "%;top:" + result + "%'  ><div class = 'eventItem' ><span  class='thing'>" + thing + "</span><br/><span class = 'start'>" + start + "</span>-<span class = 'end'>" + finish + "</span></div></div>";
                        checkDom = $(divStr).appendTo(dom).find(".eventItem")[0];
                        //创建结束
                        isCreateEvent = false;
                        eventType = "moveBottom";
                    }
                }
            } else if (eventType == "moveEvent") {
                if(mouseIsDown){
                    return;
                }
                // console.log("真正执行move")
                var temp = getTemp(dom);
                //获取高
                var height = $(checkDom).parent().height();
                // console.log("获取到的高度",height,parentHeight)
                var current = currentY / parentHeight * 100;
                var result = current - differ;
                result = calc(result, temp);
                // console.log(result);
                var _height = height / parentHeight * 100;
                var bottom = 100 - result - _height;
                if (result >= 0 && bottom >= -0.24866666666666504) {
                    var s = showTitle(result / temp, (result + _height) / temp);
                    if (getBorder(s.start, s.end, dom)) {
                        var end = $(checkDom).find(".end")[0];
                        var start = $(checkDom).find(".start")[0];
                        var finish = new Date(s.end).Format("HH:mm") == "00:00" ? "24:00":new Date(s.end).Format("HH:mm");
                        $(end).text(finish);
                        $(start).text(new Date(s.start).Format("HH:mm"));
                        $(checkDom).parent().attr("data-end", new Date(s.end).getTime());
                        $(checkDom).parent().attr("data-start", new Date(s.start).getTime());
                        setTime(checkDom); // 重新设置一下时间
                        if (bottom <= 0) {
                            bottom = 0.3;
                        }
                        if (bottom <= 2) {
                            bottom = 0
                        }
                        $(checkDom).parent().css({ 'bottom': bottom + '%', 'top': result + "%" });
                    }
                }
            }
            else if (eventType == "moveBottom") {
                //top值
                var top = mousedownY / parentHeight * 100;
                //计算出最少移动距离
                var temp = getTemp(dom);
                //根据top算出bottom总数
                var bottom = 100 - (currentY / parentHeight * 100);
                var _bootom = calc(bottom, temp);
                var _top = Number($(checkDom).parent().css("top").replace("px", "")) / parentHeight * 100;
                var s = showTitle(_top / temp, (100 - _bootom) / temp);
                // console.log(getBorder(s.start, s.end, dom))
                if (getBorder(s.start, s.end, dom)) {
                    $(checkDom).parent().css({ 'bottom': _bootom + "%", 'height': '' });
                    var end = $(checkDom).parent().find(".end")[0];
                    $(checkDom).parent().attr("data-end", new Date(s.end).getTime());
                    // console.log(new Date(s.end).Format("HH:mm"))
                    if(new Date(s.end).Format("HH:mm")=="00:00"){
                        $(end).text("24:00");
                    }else{
                        $(end).text(new Date(s.end).Format("HH:mm"));
                    }
                    setTime(checkDom); // 重新设置一下时间
                    
                }
            }else if (eventType == "moveTop") {
                //计算出最少移动距离
                var temp = getTemp(dom);
                //根据top算出bottom总数
                var top = (currentY / parentHeight * 100);
                var _top = calc(top, temp);
                // 这里需要注意一样，因为额外的添加了一个span元素，导致了，checkDom不正确了，所以这里需要两个parent
                var _bottom = Number($(checkDom).parent().css("bottom").replace("px", "")) / parentHeight * 100;
                var s = showTitle(_top / temp, (100 - _bottom) / temp);
                if (getBorder(s.start, s.end, dom)) {
                    $(checkDom).parent().css({ 'top': _top + "%", 'height': '' });
                    var start = $(checkDom).parent().find(".start")[0];
                    $(checkDom).parent().attr("data-start", new Date(s.start).getTime());
                    $(start).text(new Date(s.start).Format("HH:mm"));
                    setTime(checkDom,"top"); // 重新设置一下时间
                }
            }
        }
        // 计算开始时间和结束时间
        function showTitle(result, bottom) {
            var timestamp = new Date(new Date().toLocaleDateString()).getTime();
            var start = null;
            var end = null;
            if (result != null) {
                start = timestamp + (result.toFixed(0)) * 30 * 60 * 1000;
            }
            if (bottom != null) {
                end = timestamp + (bottom.toFixed(0)) * 30 * 60 * 1000;
            }
            return { start: start, end: end }
        }
        // 鼠标的右键事件
        $("#copyLi").click(function (e) {
            copy(e)
        })
        $("#psateLi").click(function (e) {
            paste(e);
        })
        $("#removeLi").click(function (e) {
            addDisabled();
            $(".root7").removeClass("default");
            remove_node(e);
        })
        $("#removeDayLi").click(function (e) {
            addDisabled();
            $(".root7").removeClass("default");
            remove_day(e);
        })
        $("#removeWeekLi").click(function (e) {
            addDisabled();
            remove_week(e);
        })
        $("#checkDayLi").click(function (e) {
            $(".startHour:visible").val("00");
            $(".startMinute:visible").val("00");
            $(".endHour:visible").val("24");
            $(".endMinute:visible").val("00");
            check_day(e);
            subDisabled();
        })
        
        $("#copyMToFLi").click(function (e) {
            copyMToF(e);
        })
        function remove_node(event) {
            $(checkDom).parents(".event").remove();
        }
        function remove_day(event) {
            // console.log("删除一天")
            $(root).find(".event").each(function () { $(this).remove() })
        }
        function remove_week(event) {
            for (var i = 0; i < 7; i++) {
                $(".root" + i + "").find(".event").each(function () { $(this).remove() })
            }
        }
        function check_day(event) {
            var thingAndThingColor = computerColor();
            var thing = thingAndThingColor.thing;
            var thingColor = thingAndThingColor.thingColor;
            $(root).find(".event").each(function () { $(this).remove() });
            var s = showTitle(0 / getTemp(root), 100 / getTemp(root));
            if(new Date(s.end).Format("HH:mm")=="00:00"){
                var endStr = "24:00";
            }else{
                var endStr = new Date(s.end).Format("HH:mm");
            }
            var divStr = "<div data-start= '" + new Date(s.start).getTime() + "' data-end= '" + new Date(s.end).getTime() + "' class = 'event active " + thingColor + "' style='bottom:" + 0 + "%;top:" + 0 + "%'  ><div class = 'eventItem'><span class='thing'>" + thing + "</span><br/><span class = 'start'>" + new Date(s.start).Format("HH:mm") + "</span>-<span class = 'end'>" + endStr + "</span></div></div>";
            $(root).append(divStr);
        }
        

        function copyMToF(event) {
            if($(checkDom).hasClass("eventItem")){
                var k = $(checkDom).parent().parent().data("type").replace("root", "");
            }else{
                var k = $(checkDom).parent().parent().parent().data("type").replace("root", "");
            }
            for (var i = 1; i < 6; i++) {
                if (i == k) {
                    continue;
                }
                $(".root" + i + "").find(".event").each(function () { $(this).remove() })
                $(".root" + k + "").find(".event").each(function () {
                    //获取周一到周五
                    $(".root" + i + "").append($(this).clone(true).removeClass("active"));
                })
            }
        }
        // 复制一天
        var copyContent = "";
        function copy(event) {
            copyContent = $(root).find(".event").clone();
        }

        // 粘贴一天
        function paste(event) {
            if (copyContent == "") {
                return false;
            }
            $(root).find(".event").remove();
            var this_copy = copyContent.clone();
            $(root).append(this_copy);
        }

        //获得每一份的temp
        function getTemp(dom) {
            var parentHeight = $(dom).height();
            //当前每一份的距离 px
            var temp = parentHeight / 48;
            //每一份占得比重
            var temp = temp / parentHeight * 100
            //保留两位小数
            return temp;
        }

        //计算temp的倍数
        function calc(bottom, temp) {
            var temp1 = bottom / temp;
            temp1 = parseInt(temp1);
            return temp1 * temp;
        }

        //参数dom是root节点获取当前鼠标的Y
        function getY(event, dom) {
            return event.clientY - $(dom).offset().top;
        }

        //参数dom是root子节点获取当前鼠标的Y
        function getChildY(event, dom) {
            return event.clientY - $(dom).parent().offset().top;
        }

        //判断移动是否超界
        function getBorder(_start, _end, dom) {
            var list = $(dom).find(".event:not(.active)");
            var flag = _end - _start >= 1 * 60 * 1000;
            if(!flag){
                return false;
            }
            if(new Date(_end).getHours()==0 && new Date(_end).getMinutes()==0){
                _end = 24 * 60 + 0 * 60;
            }else{
                _end = new Date(_end).getHours() * 60 + new Date(_end).getMinutes();
            }
            // 转换成小时加分钟的格式，这样话，避免了，如果昨天的时间，和今天的时间，直接对比时间戳有问题的情况
            _start = new Date(_start).getHours() * 60 + new Date(_start).getMinutes();
            for (var i = 0; i < list.length; i++) {
                var start = Number($(list[i]).attr("data-start"));
                var end = Number($(list[i]).attr("data-end"));
                if(new Date(end).getHours()==0 && new Date(end).getMinutes()==0){
                    end = 24*60 + 0*60;
                }else{
                    end = new Date(end).getHours() * 60 + new Date(end).getMinutes();
                }                
                start = new Date(start).getHours() * 60 + new Date(start).getMinutes();

                var topBorder = _start > start && _start < end;
                var bottomBorder = _end > start && _end < end;
                var isborder = _start <= start && _end >= end;

                if (topBorder || bottomBorder || isborder) {
                    // console.log("返回FALSE")
                    return false;
                }
            }
            if((_end - _start) < 60){ // 开始时间和结束时间小于一小时
                // console.log("全不显示");
                $(dom).find(".active").find(".thing").hide();
                $(dom).find(".active").find(".start").hide();
                $(dom).find(".active").find(".end").hide();
            }else if((_end - _start) > 60 && (_end - _start) < 2*60){ // 大于1小时，小于2小时
                // console.log("显示一半");
                $(dom).find(".active").find(".thing").show();
                $(dom).find(".active").find(".start").hide();
                $(dom).find(".active").find(".end").hide();
            }else{
                // console.log("全部显示")
                $(dom).find(".active").find(".thing").show();
                $(dom).find(".active").find(".start").show();
                $(dom).find(".active").find(".end").show();
            }
            // console.log("返回TRUE")
            return true;
        }
        // 计算出来现在颜色
        function computerColor() {
            var tingOut = $(".tingOut:visible").val();
            var isEmpty = $(".isEmpty:visible").prop("checked");
            var index = $(".tingOut:visible option:selected").index();
            // console.log("事件输出：",tingOut," 是否勾选空："+isEmpty,"时间表类型："+bajaObj.item.scheduleType,"index："+index);
            var thing = isEmpty ? "null" : computerThing(tingOut);
            var thingColor = "";
            if (isEmpty) {
                thingColor = "nullitem"
            } else {
                if (scheduleType == "EnumSchedule") {
                    thingColor = globalColor.list[index%11];
                } else {
                    thingColor = globalColor[tingOut]
                }
            }
            return {
                thingColor: thingColor,
                thing: thing
            }
        }
        // 计算返回的下拉框中的事件
        function  computerThing(params) { // 通过value获取到key
            var thing = "";
            if(scheduleType == "BooleanSchedule" || scheduleType== "EnumSchedule" ){
                for(var i=0;i<bajaObj.item.eventOutputs.length;i++){
                    if(params == String(bajaObj.item.eventOutputs[i]["key"])){
                        thing = bajaObj.item.eventOutputs[i]["value"];
                    } 
                }
                
            }else{
                return scheduleType == "NumericSchedule" ?  (params == "null"? "null" : params + " " +data.unit) : params;
            }
            return scheduleType == "NumericSchedule" ?  (thing == "null"? "null" : thing + " " +data.unit) : thing;
        }
        function  computerThingBack(params) { // 通过key来获取value
            var thing = "";
            if(scheduleType == "BooleanSchedule" || scheduleType== "EnumSchedule" ){
                for(var i=0;i<bajaObj.item.eventOutputs.length;i++){
                    if(params == String(bajaObj.item.eventOutputs[i]["value"])){
                        thing = String(bajaObj.item.eventOutputs[i]["key"]);
                    } 
                }
            }else{
                return params;
            }
            return thing;
        }

        // 小时和分钟添加时间
        addHourList();
        addMinuteList();
        setTimeNow();
        function addHourList() {
            var str = "";
            for (var i = 0; i < 25; i++) {
                if (i < 10) {
                    i = "0" + i;
                }
                str += "<option value='" + i + "'>" + i + languageData.hour +"</option>"
            }
            $(".hourList").empty().append(str);
        }
        function addMinuteList() {
            var str = "";
            for (var i = 0; i < 60; i++) {
                if (i < 10) {
                    i = "0" + i;
                }
                str += "<option value='" + i + "'>" + i + languageData.min + "</option>"
            }
            $(".minuteList").empty().append(str);
        }
        // 让时间设置为现在的的时间
        function setTimeNow(){
            var now = new Date();
            var hour = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
            var min = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
            $(".startHour").val(hour);
            $(".startMinute").val(min);
            $(".endHour").val(hour);
            $(".endMinute").val(min);
        }
        // 设置下面的时间和选中的块时间联动
        function setTime(dom) { // dom为外层的DIV
            // console.log("开始执行设置时间",dom,dom.target)
            var isTop = false;
            if(isTop){
                if($(dom).hasClass("thing")||$(dom).hasClass("start")||$(dom).hasClass("end")){
                    var startTime = $(dom).parent().find(".start").text();
                    var endTime = $(dom).parent().find(".end").text();
                }
            }else{
                if ("target" in dom && $(dom.target).hasClass("eventItem")) {
                    var startTime = $(dom.target).find(".start").text();
                    var endTime = $(dom.target).find(".end").text();
                }else if("target" in dom){
                    var startTime = $(dom.target).parent().find(".start").text();
                    var endTime = $(dom.target).parent().find(".end").text();
                } else {
                    if($(dom).hasClass("thing")||$(dom).hasClass("start")||$(dom).hasClass("end")){
                        var startTime = $(dom).parent().find(".start").text();
                        var endTime = $(dom).parent().find(".end").text();
                    }else{
                        var startTime = $(dom).find(".start").text();
                        var endTime = $(dom).find(".end").text();
                    }
                    
                }
            }
            
            // console.log("获取的开始时间和结束时间",startTime,endTime);
            var startHour = startTime.split(":")[0];
            var startMinute = startTime.split(":")[1];
            var endHour = endTime.split(":")[0];
            var endMinute = endTime.split(":")[1];
            $(".startHour:visible").val(startHour).attr("disabled",false);
            $(".startMinute:visible").val(startMinute).attr("disabled",false);
            $(".endHour:visible").val(endHour).attr("disabled",false);
            $(".endMinute:visible").val(endMinute).attr("disabled",false);
            $(".isEmpty:visible").attr("disabled",false);
            $(".tingOut:visible").attr("disabled",false);
            if(endHour==24){
                $(".endMinute:visible").attr("disabled",true);
            }
            changeTitle($(".active:not('.blocksRoot')"));
        }
        // 设置下面的事件输入联动
        function setColor(dom) {
            // console.log(dom);
            if ("target" in dom && $(dom.target).hasClass("eventItem")) {
                var thing = $(dom.target).find(".thing").text();
            }else if("target" in dom ){
                var thing = $(dom.target).parent().find(".thing").text();
            }else {
                var thing = $(dom).find(".thing").text();
            }
            // console.log(thing);
            if(scheduleType=="NumericSchedule"){
                if(thing.indexOf("null")!=-1){
                    thing = "null";
                }else{
                    thing = parseFloat(thing);
                }
            }
            // console.log(thing);
            if (thing == "null") {
                $(".isEmpty:visible").prop("checked", true);
                $(".tingOut:visible").attr("disabled", true);
            } else {
                $(".isEmpty:visible").prop("checked", false);
                $(".tingOut:visible").attr("disabled", false).val(computerThingBack(thing));
            }
        }

        // 更改事件输出
        var selectColor = "";
        $(".tingOut").change(function () {
            var thingAndThingColor = computerColor();
            var thing = thingAndThingColor.thing;
            selectColor = thingAndThingColor.thingColor;
            removeClassFun();
            $(".active:not('.blocksRoot')").addClass(selectColor);
            $(".active:not('.blocksRoot')").find(".thing").text(thing);
            changeTitle($(".active:not('.blocksRoot')"));
        })
        $(".tingOut").on("input",function(){
            // console.log("input 触发了输入事件")
            var thingAndThingColor = computerColor();
            var thing = thingAndThingColor.thing;
            selectColor = thingAndThingColor.thingColor;
            removeClassFun();
            $(".active:not('.blocksRoot')").addClass(selectColor);
            $(".active:not('.blocksRoot')").find(".thing").text(thing);
            changeTitle($(".active:not('.blocksRoot')"));
        })
        $(".isEmpty").change(function () {
            // console.log($(this).prop("checked"));
            if ($(this).prop("checked")) {
                selectColor = "null";
                $(".tingOut:visible").attr("disabled", true);
                removeClassFun();
                $(".active:not('.blocksRoot')").addClass(globalColor[selectColor]);
                $(".active:not('.blocksRoot')").find(".thing").text("null");
            } else {
                $(".tingOut:visible").attr("disabled", false);
                if($(".tingOut:visible").val()==""){
                    $(".tingOut:visible").find("option").eq(0).prop("selected",true);
                }
                
                var thingAndThingColor = computerColor();
                var thing = thingAndThingColor.thing;
                selectColor = thingAndThingColor.thingColor;
                removeClassFun();
                $(".active:not('.blocksRoot')").addClass(selectColor);
                $(".active:not('.blocksRoot')").find(".thing").text(thing);
            }
            changeTitle($(".active:not('.blocksRoot')"));
        })
        // 去除添加上的Class
        function removeClassFun() {
            for (i in globalColor) {
                if (i != "list") {
                    if (globalColor[i]) {
                        if ($(".active:not('.blocksRoot')").hasClass(globalColor[i])) {
                            $(".active:not('.blocksRoot')").removeClass(globalColor[i])
                        }
                    }
                } else { // 如果是在list中的数组，重新跑一层循环
                    for (var j = 0; j < globalColor[i].length; j++) {
                        if (globalColor[i][j]) {
                            if ($(".active:not('.blocksRoot')").hasClass(globalColor[i][j])) {
                                $(".active:not('.blocksRoot')").removeClass(globalColor[i][j])
                            }
                        }
                    }
                }

            }
        }
        // 更改title
        function changeTitle($dom){
            // console.log($dom);
            var thing = $dom.find(".thing").text();
            var start = $dom.find(".start").text();
            var end = $dom.find(".end").text();
            $dom.attr("title",thing + "\n" + start + "-" + end);
        }

        // 更改移动滑块的开始时间和结束时间
        $(".startHour").change(function () {
            if ($(this).val() == 24) {
                $(".startMinute:visible").val("00").attr("disabled", true);
            } else {
                $(".startMinute:visible").attr("disabled", false);
            }

            if ($(".active:not('.blocksRoot')").length != 0) {
                changeTime($(".active:not('.blocksRoot')"));
            }
        })
        $(".startMinute").change(function () {
            if ($(".active:not('.blocksRoot')").length != 0) {
                changeTime($(".active:not('.blocksRoot')"));
            }
        })
        $(".endHour").change(function () {
            if ($(this).val() == 24) {
                $(".endMinute:visible").val("00").attr("disabled", true);
            } else {
                $(".endMinute:visible").attr("disabled", false);
            }
            if ($(".active:not('.blocksRoot')").length != 0) {
                changeTime($(".active:not('.blocksRoot')"));
            }
        })
        $(".endMinute").change(function () {
            if ($(".active:not('.blocksRoot')").length != 0) {
                changeTime($(".active:not('.blocksRoot')"));
            }
        })
        function changeTime(dom) {
            var startHour = $(".startHour:visible").val();
            var startMinute = $(".startMinute:visible").val();
            var endHour = $(".endHour:visible").val();
            if (endHour == 24) {
                $(".endMinute:visible").val("00")
            }
            var endMinute = $(".endMinute:visible").val();
            // console.log(startHour, startMinute, endHour, endMinute);
            var startTime = new Date();
            startTime.setHours(startHour);
            startTime.setMinutes(startMinute);
            startTime.setSeconds(0);
            startTime.setMilliseconds(0);
            var endTime = new Date();
            endTime.setHours(endHour);
            endTime.setMinutes(endMinute);
            endTime.setSeconds(0);
            endTime.setMilliseconds(0);
            // console.log(startTime.getTime(), endTime.getTime());
            var isCanChange = getBorder(startTime.getTime(), endTime.getTime(), dom.parent()[0]);
            if (isCanChange) {
                var thisDiv = $(".active:not('.blocksRoot')");
                thisDiv.find(".start").text(startHour + ":" + startMinute);
                thisDiv.find(".end").text(endHour + ":" + endMinute);
                var data_start = "";
                if (startMinute > 0 && startMinute < 15) {
                    data_start = 0;
                } else if (startMinute >= 15 && startMinute <= 45) {
                    data_start = 30;
                } else if (startMinute > 45) {
                    data_start = 0;
                    startHour++; // 这里缺少一个临界值的判断
                }
                var data_end = "";
                if (endMinute > 0 && endMinute < 15) {
                    data_end = 0;
                } else if (endMinute >= 15 && endMinute <= 45) {
                    data_end = 30;
                } else if (endMinute > 45) {
                    data_end = 0;
                    endHour++; // 这里缺少一个临界值的判断
                }
                thisDiv.attr("data-start", startTime.getTime());
                thisDiv.attr("data-end", endTime.getTime());
                var topValue = (100 * (startHour * 60 + data_start) / 1440).toFixed(4) + "%";
                var bottomValue = (100 * (1440 - endHour * 60 - data_end) / 1440).toFixed(4) + "%";
                // console.log("计算出来的top和bottom", topValue, bottomValue)
                thisDiv.css("bottom", bottomValue);
                thisDiv.css("top", topValue);
            } else {
                // alert(languageData.DateError);
                $.message({
                    type:"warning",
                    message:languageData.DateError
                })
                var startHour = dom.find(".start").text().split(":")[0];
                var startMinute = dom.find(".start").text().split(":")[1];
                var endHour = dom.find(".end").text().split(":")[0];
                var endMinute = dom.find(".end").text().split(":")[1];
                $(".startHour:visible").val(startHour);
                $(".startMinute:visible").val(startMinute);
                $(".endHour:visible").val(endHour);
                $(".endMinute:visible").val(endMinute);
            }
            changeTitle($(".active:not('.blocksRoot')"))
        }

        function addDisabled(){
            $(".startHour").attr("disabled",true);
            $(".startMinute").attr("disabled",true);
            $(".endHour").attr("disabled",true);
            $(".endMinute").attr("disabled",true);
            $(".tingOut").attr("disabled",true);
            $(".isEmpty").attr("disabled",true);
        }
        function subDisabled(){
            $(".startHour").attr("disabled",false);
            $(".startMinute").attr("disabled",false);
            $(".endHour").attr("disabled",false);
            $(".endMinute").attr("disabled",false);
            $(".tingOut").attr("disabled",false);
            $(".isEmpty").attr("disabled",false);
        }
    }
    return {
        load1: load1
    }
});

